﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading.Tasks;
using XiaoYuStock.Core;
using XiaoYuStock.Core.Entities;
using XiaoYuStockAnalysis.BusinessLogic.Business;
using XiaoYuStock.Model.Models;

namespace XiaoYuStock.WebApp.Api
{
    public class ExternalSeverController : ApiController
    {
        private static string EncryptKey = "G8670$67";


        /// <summary>
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> GetMultipleData(dynamic obj)
        {
            var ret = new ApiResult<string>();
            try
            {
                string req= obj.req;

                if (string.IsNullOrEmpty(req))
                {
                    return new ApiResult<string>().Error("输入参数内容不完整");
                }
                string Content = CryptoHelper.Decrypt(req, EncryptKey);
                string htmlContent = RequestHelp.Get(Content);
                var jsonData= JsonHelper.ToJson(TransferDataHelper.ConvertToObjs(htmlContent));
                ret.Data = CryptoHelper.Encrypt(jsonData, EncryptKey);
             
            }
            catch (Exception ex)
            {
                return new ApiResult<string>().Error(ex.Message);
            }
            return ret;
        }

        [HttpPost]
        public ApiResult<string> GetPlateData(dynamic obj)
        {
            var ret = new ApiResult<string>();
            try
            {
                string req = obj.req;
                if (string.IsNullOrEmpty(req))
                {
                    return new ApiResult<string>().Error("输入参数内容不完整");
                }
                string Content = CryptoHelper.Decrypt(req, EncryptKey);
                string htmlContent = RequestHelp.Get(Content);
                var jsonData = JsonHelper.ToJson(PlateDataHelper.ConvertToObj(htmlContent));
                ret.Data = CryptoHelper.Encrypt(jsonData, EncryptKey);

            }
            catch (Exception ex)
            {
                return new ApiResult<string>().Error(ex.Message);

            }
            return ret;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> GetSyncData(dynamic obj)
        {
            var ret = new ApiResult<string>();
            try
            {
                int id = obj.id;
                string code = obj.code;
                LogHelper.Info("GetSyncData->id="+id+",code="+code);
                HistoryDataBusiness historyBus = new HistoryDataBusiness();
                var result = historyBus.GetSyncList(id, code);
                return new ApiResult<string>().Success(JsonHelper.ToJson(result));
            }


            catch (Exception ex)
            {
                return new ApiResult<string>().Error(ex.Message);

            }
        }
    }
}
